﻿using System;
using System.Windows.Forms;
using HighDiveEngine.Aggregates;
using HighDiveEngine.Model;
using HighDiveEngine.Resources;
using System.Text.RegularExpressions;

namespace HighDiveAdmin.GUI.Components
{
    public partial class EditContestNewJudgePanel : UserControl
    {
        public event DelegateVoidJudgeEventArgs EventSaveJudge;
        public event DelegateVoidString EventLoadJudge;
        public event MethodInvoker EventDiscard;
        
        public EditContestNewJudgePanel()
        {
            InitializeComponent();
            
        }

        private void textBoxNewJudgeLastName_TextChanged(object sender, EventArgs e)
        {

        }

        public void Show(Judge judge)
        {
            if (judge.IdNumber != null)
            {
                textBoxFirstName.Enabled = false;
                textBoxLastName.Enabled = false;
                comboBoxGender.Enabled = false;
                textBoxAddress.Enabled = true;
                textBoxEmail.Enabled = true;
                textBoxClub.Enabled = true;

                textBoxIdNumber.Text = judge.IdNumber;
                textBoxFirstName.Text = judge.FirstName;
                textBoxLastName.Text = judge.LastName;
                textBoxAddress.Text = judge.Address;
                textBoxEmail.Text = judge.Email;
                textBoxClub.Text = judge.Club;                
            }
            else
            {
                textBoxIdNumber.Enabled = true;
                textBoxFirstName.Enabled = false;
                textBoxLastName.Enabled = false;
                comboBoxGender.Enabled = false;
                textBoxClub.Enabled = false;
                textBoxAddress.Enabled = false;
                textBoxEmail.Enabled = false;
            }
            labelNumber.Text = judge.JudgeNumber.ToString();
            Visible = true;
        }

        private void buttonDiscard_Click(object sender, EventArgs e)
        {
            textBoxIdNumber.Text = "";
            _clearText();
            Visible = false;
            if (EventDiscard != null)
                EventDiscard();
        }

        internal void FillJudgeInfo(PersonList personList)
        {
            var strcol = new AutoCompleteStringCollection();
            foreach (var person in personList)
            {
                if (person.IdNumber != null)
                    strcol.Add(person.IdNumber);
            }
            textBoxIdNumber.AutoCompleteCustomSource = strcol;
        }

        internal void FillExistingJudgeFromIdString(Person person)
        {
            textBoxClub.Text = person.Club;
            textBoxFirstName.Text = person.FirstName;
            textBoxLastName.Text = person.LastName;
            textBoxAddress.Text = person.Address;
            textBoxEmail.Text = person.Email;
        }

        private void textBoxIdNumber_Leave(object sender, EventArgs e)
        {
            if (new Regex(@"^[0-9]{10}$").IsMatch(textBoxIdNumber.Text))
            {
                _clearText();

                try
                {
                    textBoxFirstName.Enabled = true;
                    textBoxLastName.Enabled = true;
                    comboBoxGender.Enabled = true;

                    //attempt load from db
                    if (EventLoadJudge != null)
                        EventLoadJudge(textBoxIdNumber.Text);

                    textBoxClub.Enabled = true;
                    textBoxAddress.Enabled = true;
                    textBoxEmail.Enabled = true;

                    comboBoxGender.Text = "Male";

                    //set focus
                    if (comboBoxGender.Enabled)
                        comboBoxGender.Focus();
                    else
                        textBoxAddress.Focus();
                }
                catch (Exception exception)
                {
                    textBoxFirstName.Enabled = false;
                    textBoxLastName.Enabled = false;
                    comboBoxGender.Enabled = false;
                    toolTipError.Show(exception.Message, textBoxIdNumber, 30, 20, 5000);
                }
            }
        }

        private void _clearText()
        {
            textBoxAddress.Text = "";
            textBoxClub.Text = "";
            textBoxEmail.Text = "";
            textBoxFirstName.Text = "";
            textBoxLastName.Text = "";
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            #region Regex Checks
            if (!(new Regex(@"^[0-9]{10}$").IsMatch(textBoxIdNumber.Text)))
            {
                MessageBox.Show("Invalid social security number");
                return;
            }
            if (!(new Regex(@"^[a-zA-ZåäöüÅÄÖ]+$").IsMatch(textBoxFirstName.Text)))
            {
                MessageBox.Show("Please enter a valid first name");
                return;
            }
            if (!(new Regex(@"^[a-zA-ZåäöüÅÄÖ]+$").IsMatch(textBoxLastName.Text)))
            {
                MessageBox.Show("Please enter a valid last name");
                return;
            }
            if (!(new Regex(@"^([a-zA-ZåäöüÅÄÖ 0-9]+)?$").IsMatch(textBoxAddress.Text)))
            {
                MessageBox.Show("Please enter a valid adress");
                return;
            }
            if (!(new Regex(@"^([A-Za-z0-9]+@([A-Za-z0-9]+\.)+[A-Za-z]{2,3})?$").IsMatch(textBoxEmail.Text)))
            {
                MessageBox.Show("Please enter a valid email adress");
                return;
            }
            if (!(new Regex(@"^([a-zA-ZåäöüÅÄÖ 0-9]+)?$").IsMatch(textBoxClub.Text)))
            {
                MessageBox.Show("Please enter a valid club");
                return;
            }

            #endregion

            Visible = false;
            var judgeArgs = new JudgeEventArgs()
            {
                Address = textBoxAddress.Text,
                Club = textBoxClub.Text,
                Email = textBoxEmail.Text,
                FirstName = textBoxFirstName.Text,
                Gender = (Gender)Enum.Parse(typeof(Gender), comboBoxGender.Text),
                IdNumber = textBoxIdNumber.Text,
                LastName = textBoxLastName.Text,
            };


            if (EventSaveJudge != null)
            {
                textBoxIdNumber.Text = "";
                _clearText();
                EventSaveJudge(judgeArgs);
            }
                
        }

        #region TabStop
        private void EditContestNewJudgePanel_TabStopChanged(object sender, EventArgs e)
        {
            foreach (Control control in Controls)
                control.TabStop = TabStop;
            if (TabStop)
                textBoxIdNumber.Focus();
        }

        private void EditContestNewJudgePanel_VisibleChanged(object sender, EventArgs e)
        {
            if (Visible)
                textBoxIdNumber.Focus();
        } 
        #endregion
    }
}
